<?php if(!defined('BASE_PATH')) exit('No Access!');
/////////////////////////////////////////////////////////////////////////////
//
// SuperPHP - 快速的中文PHP框架
//
// @package		SuperPHP
// @author		hmw@superphp.org
// @copyright	Copyright (c) 2010, SuperPHP.ORG
// @license		http://www.superphp.org/user_guide/license.html
// @link		http://www.superphp.org
// @since		Version 1.0
//
// 许可协议请查看 http://www.SuperPHP.org/
//
/////////////////////////////////////////////////////////////////////////////

define('SUPER_VERSION', '1.0'); // 当前框架版本

/**
 * SuperPHP应用框架的系统执行程序
 */

// 载入核心函数库
require(SUPER_PATH."/Common.php");

//如果运行环境不是PHP5.0以上，报错，程序终止执行
if (substr(PHP_VERSION, 0, 1) != '5')showError("The SuperPHP Framework Require PHP5!");

// 载入配置文件
$GLOBALS['SUPER'] = spConfigReady(require(SUPER_PATH."/Config.php"),$config);

// 根据配置文件进行一些全局变量的定义
if('debug' == $GLOBALS['SUPER']['mode']){
	define("SUPER_DEBUG",TRUE); // 当前正在调试模式下
}else{
	define("SUPER_DEBUG",FALSE); // 当前正在部署模式下
}

// 设置错误处理函数
set_error_handler("showErrorHandler");
@set_magic_quotes_runtime(0);

//将当前项目路径加入项目文件搜索目录
set_include_path(SUPER_PATH . PATH_SEPARATOR . get_include_path());

// 自动开启SESSION
if($GLOBALS['SUPER']['auto_session'])session_start();

//载入类加载器
//echo get_include_path();exit;
import($GLOBALS['SUPER']["sp_core_path"]."/Loader.php", FALSE, TRUE);

//加载系统class注册类
Super_Loader::loadClass('Super_Registry');

//加载系统配置类
//Super_Loader::loadClass('Super_Config');

$registry = Super_Registry::getInstance();
//$configObj = new Super_Config;
//$registry->set('config',$configObj);
$db = &Db($GLOBALS['SUPER']['db']);
$registry->set('db',$db);

//如果开启缓存，加载系统缓存类,声明并注册到系统内部
if(TRUE === $GLOBALS['SUPER']['cache']['enabled']){
    Super_Loader::loadClass('Super_Cache');
    $cache = new Super_Cache($GLOBALS['SUPER']['cache']);
    $registry->set('cache',$cache);
}

//如果开启模板系统，加载view类，声明并注册到系统内部
if(TRUE === $GLOBALS['SUPER']['view']['enabled']){
    Super_Loader::loadClass('Super_View');
    $view = new Super_View($GLOBALS['SUPER']['view']);
    $registry->set('view',$view);
}

//如果开启静态，加载Html类，声明并注册
if(TRUE === $GLOBALS['SUPER']['html']['enabled']){
    Super_Loader::loadClass('Super_Html');
    $html = new Super_Html($GLOBALS['SUPER']['html']);
    $registry->set('html',$html);
}

// 载入核心MVC架构文件
import($GLOBALS['SUPER']["sp_core_path"]."/Controller.php", FALSE, TRUE);
import($GLOBALS['SUPER']["sp_core_path"]."/Model.php", FALSE, TRUE);

// 当在二级目录中使用SuperPHP框架时，自动获取当前访问的文件名
if("/index.php" == $GLOBALS['SUPER']['url']["url_path_base"]){
	if(basename($_SERVER['SCRIPT_NAME']) === basename($_SERVER['SCRIPT_FILENAME']))
		$GLOBALS['SUPER']['url']["url_path_base"] = $_SERVER['SCRIPT_NAME'];
	elseif (basename($_SERVER['PHP_SELF']) === basename($_SERVER['SCRIPT_FILENAME']))
		$GLOBALS['SUPER']['url']["url_path_base"] = $_SERVER['PHP_SELF'];
	elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === basename($_SERVER['SCRIPT_FILENAME']))
		$GLOBALS['SUPER']['url']["url_path_base"] = $_SERVER['ORIG_SCRIPT_NAME'];
}

// 在使用PATH_INFO的情况下，对路由进行预处理
if(TRUE == $GLOBALS['SUPER']['url']["url_path_info"] && !empty($_SERVER['PATH_INFO'])){
	$url_args = explode("/", $_SERVER['PATH_INFO']);$url_sort = array();
	$args_count = count($url_args);
	//print_r($url_args);exit;
	for($u = 1; $u < $args_count; $u++){
		if(trim($url_args[$u]) == '') continue;
		if(TRUE === $GLOBALS['SUPER']['enable_module']){
			if($u == 1)$url_sort[$GLOBALS['SUPER']['url_module']] = $url_args[$u];
			elseif($u == 2)$url_sort[$GLOBALS['SUPER']["url_controller"]] = $url_args[$u];
			elseif($u == 3)$url_sort[$GLOBALS['SUPER']["url_action"]] = $url_args[$u];
			else {$url_sort[$url_args[$u]] = isset($url_args[$u+1]) ? $url_args[$u+1] : "";$u+=1;}
		} else {
			if($u == 1)$url_sort[$GLOBALS['SUPER']["url_controller"]] = $url_args[$u];
			elseif($u == 2)$url_sort[$GLOBALS['SUPER']["url_action"]] = $url_args[$u];
			else {$url_sort[$url_args[$u]] = isset($url_args[$u+1]) ? $url_args[$u+1] : "";$u+=1;}
		}
	}
	if("POST" == strtoupper($_SERVER['REQUEST_METHOD'])) $_REQUEST = $_POST =  $_POST + $url_sort;
	else $_REQUEST = $_GET = $_GET + $url_sort;
}

// 构造执行路由
$__module = isset($_REQUEST[$GLOBALS['SUPER']["url_module"]]) ?
	$_REQUEST[$GLOBALS['SUPER']["url_module"]] :
	$GLOBALS['SUPER']["default_module"];
$__controller = isset($_REQUEST[$GLOBALS['SUPER']["url_controller"]]) ?
	$_REQUEST[$GLOBALS['SUPER']["url_controller"]] :
	$GLOBALS['SUPER']["default_controller"];
$__action = isset($_REQUEST[$GLOBALS['SUPER']["url_action"]]) ?
	$_REQUEST[$GLOBALS['SUPER']["url_action"]] :
	$GLOBALS['SUPER']["default_action"];

// 对路由进行自动执行相关操作
superLaunch("router_prefilter");

$GLOBALS['SUPER']['current_module']	= $__module;
$GLOBALS['SUPER']['controller_path'] = BASE_PATH.'/'.$__module.'/'.$GLOBALS['SUPER']["controller_path"];
$GLOBALS['SUPER']['model_path'] = BASE_PATH.'/'.$__module.'/'.$GLOBALS['SUPER']['model_path'];

$handle_controller = loadClass($__controller, null, $GLOBALS['SUPER']["controller_path"].'/'.$__controller.".php");
// 调用控制器出错将调用路由错误处理函数
if(!is_object($handle_controller) || !method_exists($handle_controller, $__action)){
	eval($GLOBALS['SUPER']["dispatcher_error"]);
	exit;
}

// 执行用户代码
$handle_controller->$__action();

// 控制器程序运行完毕，进行模板的自动输出
if(FALSE != $GLOBALS['SUPER']['view']['auto_display']){
	$__tplname = $GLOBALS['SUPER']['view']['config']['template_dir']."/".
		$__controller.$GLOBALS['SUPER']['view']['auto_display_sep'].
			$__action.$GLOBALS['SUPER']['view']['auto_display_suffix'];
	$handle_controller->v->auto_display($__tplname);
}